/**
 * 
 */
package cn.mxz.robots.province;

import java.util.Scanner;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import cn.mxz.crawler_single.basic.Request;
import cn.mxz.rds.pojo.GaoKaoProvince;
import cn.mxz.rds.pojo.GaoKaoRank;
import cn.mxz.rds.service.GaoKaoProvinceService;
import cn.mxz.rds.service.GaoKaoRankService;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.basic.local.IRank;
import cn.mxz.robots.pojo.Student;

/**
 * 贵州省
 * 排名:完成
 * 去年排名：完成
 * 
 * @author cuizhennan<cuizhennan@mouge.wang>
 * @since 2015-06-22
 */
public class Guizhou extends Exam implements IRank
{
	public static void main(String[] args)
	{
		Student student = new Student("705200071", "UTZSJWNY", "62282619930527", "2826152803", BEN_KE_LI_KE);
		Guizhou gansu = new Guizhou(student);
		System.out.println(gansu.getCaptcha());
		System.out.println("请输入验证码：");
		@SuppressWarnings("resource")
		String captcha = new Scanner(System.in).next();
		System.out.println(gansu.login(captcha));

	}

	public Guizhou(Student student)
	{
		super(student);
	}

	@Override
	public String getCaptcha()
	{
		Request loginRequest = new Request("http://222.85.136.23/ptgk/gkzy23.asp", mSha1);
		Document paraDocument = Jsoup.parse(curlPost(loginRequest));
		setCache("id" + mSha1, paraDocument.select("input[name=id]").val());
		setCache("dmlx" + mSha1, paraDocument.select("input[name=dmlx]").val());
		setCache("va" + mSha1, paraDocument.select("input[name=va]").val());
		String src = paraDocument.select("img").attr("src");
		String captchaUrl = "http://222.85.136.23" + src;
		Request captchaRequest = new Request(captchaUrl, mSha1);
		// referer http://222.85.136.23/ptgk/gkzy23.asp
		return curlFile(captchaRequest);
	}

	@Override
	public String login(String captcha)
	{
		Request loginRequest = new Request("http://222.85.136.23/ptgk/gkzy23.asp", mSha1);
		loginRequest.setCharSet("gb2312");
		loginRequest.addHeader("Referer", "http://222.85.136.23/ptgk/gkzy23.asp");
		loginRequest.addParams("id", getCache("id" + mSha1));
		loginRequest.addParams("dmlx", getCache("dmlx" + mSha1));
		loginRequest.addParams("va", getCache("va" + mSha1));
		loginRequest.addParams("code", mStudent.getStudentId());
		loginRequest.addParams("mm", mStudent.getStudentPwd());
		loginRequest.addParams("yzm", captcha);
		loginRequest.addParams("ty", "1");
		loginRequest.addParams("action.x", "73");
		loginRequest.addParams("action.y", "15");

		String loginHtml = curlPost(loginRequest);
		JSONObject resultObject = new JSONObject();

		if (loginHtml.contains("欢迎您"))
		{
			Document scoreDocument = Jsoup.parse(loginHtml);
			// System.out.println(scoreDocument.select("a").get(9).attr("href"));
			String src = regexMatch(scoreDocument.select("a:contains(成绩查询)").get(0).attr("href"), "'(.*)',");
			// String src =
			// regexMatch(scoreDocument.select("a").get(9).attr("href"),
			// "'(.*)',");
			String scoreUrl = "http://222.85.136.23" + src;
			Request scoreRequest = new Request(scoreUrl, mSha1);
			scoreRequest.addHeader("Referer", "http://222.85.136.23/ptgk/gkzy23.asp");
			loginHtml = curlPost(scoreRequest);
			// System.out.println("欢迎您： "+loginHtml);
			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(loginHtml));
		}
		else if (loginHtml.contains("准考证号或密码"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", NOT_EXIST_USER);
		}
		else if (loginHtml.contains("验证码输入错误"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_CAPTCHA);
		}
		else
		{
			System.out.println("内部错误： " + loginHtml);
			resultObject.put(ERROR, INTERNAL_SERVER_ERROR);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String parseHtml)
	{
		Document loginDocument = Jsoup.parse(parseHtml);
		JSONObject result = new JSONObject();
		Elements trsif = loginDocument.select("table.TableBlock").get(0).select("tr");
		result.put("exam_id", trsif.get(1).select("td").get(1).text());
		result.put("name", trsif.get(3).select("td").get(1).text());
		result.put("ranking", getRank(8 + 31, trsif.get(5).select("td").get(1).text(), mStudent.getType()));
		result.put("province", "贵州省");
		JSONObject scoreObject = new JSONObject();
		scoreObject.put("语文", trsif.get(6).select("td").get(1).text());
		scoreObject.put("数学", trsif.get(7).select("td").get(1).text());
		scoreObject.put("综合", trsif.get(9).select("td").get(1).text());
		scoreObject.put("英语", trsif.get(8).select("td").get(1).text());
		scoreObject.put("总分", trsif.get(5).select("td").get(1).text());
		scoreObject.put("政策加分", trsif.get(11).select("td").get(1).text());

		result.put("score", scoreObject.toString());
		return result.toString();
	}

	@Override
	public void initRank()
	{
		GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
		GaoKaoProvince province = provinceService.get(8 + 31);
		GaoKaoRankService rankService = new GaoKaoRankService();

		// 理科 http://www.gxeduw.com/gaokao/2015/124204_4.html
		String rankHtml = curlGet(new Request("http://www.gxeduw.com/gaokao/2015/124205_4.html"));
		// 文科 http://www.gxeduw.com/gaokao/2015/124205.html
		Document rankDom = Jsoup.parse(rankHtml);
		Elements as = rankDom.select("table").get(0).select("tr");
		Integer pre = 127094;
		int asSize = as.size();
		for (int i = 2; i < asSize; i++)
		{
			String scoreString = as.get(i).select("td").get(0).text();
			String countString = as.get(i).select("td").get(1).text();
			String leiji = as.get(i).select("td").get(2).text();

			Integer count = Integer.parseInt(countString);
			Integer ranking = pre + 1;
			pre = Integer.parseInt(leiji);

			Integer score = Integer.parseInt(scoreString);
			GaoKaoRank rank = new GaoKaoRank();

			rank.setScore(score);
			rank.setCount(count);
			rank.setType(BEN_KE_WEN_KE);
			rank.setGaoKaoProvince(province);
			rank.setRanking(ranking);

			rankService.save(rank);
			System.out.println("score: " + scoreString + "count: " + countString + "rank: " + ranking);
			System.out.println(rank);
		}
	}

	@Override
	public void lastYearRank()
	{
		GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
		GaoKaoProvince province = provinceService.get(8);
		GaoKaoRankService rankService = new GaoKaoRankService();

		// 理科 http://www.gxeduw.com/gaokao/2014/107900.html
		String rankHtml = curlGet(new Request("http://www.gxeduw.com/gaokao/2014/107900.html"));
		// 文科 http://www.gxeduw.com/gaokao/2014/107902.html
		Document rankDom = Jsoup.parse(rankHtml);
		Elements as = rankDom.select("table").get(0).select("tr");
		Integer pre = 0;
		int asSize = as.size();
		for (int i = 1; i < asSize; i++)
		{
			String scoreString = regexMatch(as.get(i).select("td").get(0).text(), "(\\d+)");
			String countString = regexMatch(as.get(i).select("td").get(1).text(), "(\\d+)");
			String leiji = regexMatch(as.get(i).select("td").get(2).text(), "(\\d+)");

			if (scoreString.equals("") || leiji.equals(""))
				continue;
			Integer count = Integer.parseInt(countString);
			Integer ranking = pre + 1;
			pre = Integer.parseInt(leiji);

			Integer score = Integer.parseInt(scoreString);
			GaoKaoRank rank = new GaoKaoRank();

			rank.setScore(score);
			rank.setCount(count);
			rank.setType(BEN_KE_LI_KE);
			rank.setGaoKaoProvince(province);
			rank.setRanking(ranking);

			rankService.save(rank);
			System.out.println("score: " + scoreString + "count: " + countString + "rank: " + ranking);
			System.out.println(rank);
		}
	}
}
